<template>
  <div>
    <input type="text" :value="value" style="display: none" />
    <el-select
      v-model="selected"
      :clearable="true"
      :size="size"
      :placeholder="placeholder"
      @change="handleItemChange"
    >
      <el-option
        v-for="item in orgList"
        :key="item.datum_customer_shareholder_id"
        :label="item.real_name"
        :value="item.datum_customer_shareholder_id"
      ></el-option>
    </el-select>
  </div>
</template>

<script>
export default {
  components: {},
  props: {
    value: {
      type: String,
      default: '',
    },
    size: {
      type: String,
      default: '',
    },
    placeholder: {
      type: String,
      default: '请选择',
    },
  },
  data() {
    return {
      selected: null,
      orgList: [],
      id: '',
    }
  },
  computed: {},
  watch: {
    value(val) {
      this.selected = val
    },
  },
  created() {
    this.id = this.$route.query.id || sessionStorage.getItem('customer_related_id')
    this.selected = this.value
    this.getData()
  },
  mounted() {},
  methods: {
    getData() {
      this.$api
        .getData('credit', 'datum_customer_shareholder', {
          'customer_related_id.eq': this.id,
        })
        .then(res => {
          if (!res || !res.data) {
            return
          }
          this.orgList = res.data
          this.orgList.push({
            real_name: res.data[0].customer_name,
            datum_customer_shareholder_id: res.data[0].customer_id,
          })
        })
    },
    handleItemChange(val) {
      const index = this.orgList.findIndex(it => it.datum_customer_shareholder_id === val)
      if (index > -1) {
        this.$emit('getname', this.orgList[index].real_name)
      }
      this.$emit('input', val)
      this.$emit('change', val)
    },
  },
}
</script>

<style scoped></style>
